package com.marisfrolg.util;

import com.iData.barcodecontroll.BarcodeControll;

import android.os.Handler;
import android.os.Message;


public class HadwareControll implements ReadCodeController{
	private static BarcodeControll barcodeControll = new BarcodeControll();
	private static Handler m_handler = null;
	private static boolean m_stop = false;
	private static boolean start_Scan=false;
	public  void Open() {
		barcodeControll.Barcode_open();
		m_stop = false;
		start_Scan=false;
		new BarcodeReadThread().start();
	}

	public  void Close() {
		m_stop = true;
		start_Scan=false;
		barcodeControll.Barcode_Close();
	}

	public  void scan_start() {	
		barcodeControll.Barcode_StartScan();
		start_Scan=true;
	}

	public  void scan_stop() {

		barcodeControll.Barcode_StopScan();
	}

	static class BarcodeReadThread extends Thread {
		public void run() {
			while (!m_stop) {
				try {
					Thread.sleep(50);
					ReadBarcodeID();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
	}

	public static void ReadBarcodeID() {
		String info = null;
		byte[] buffer = null;
		
		 buffer = barcodeControll.Barcode_Read();
		try {
			
		info = new String(buffer, "GBK");
		}catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (info.length() > 0 && m_handler != null&&start_Scan) {
		
			Message msg = new Message();
			msg.what = CODEREAD;
			msg.obj = info;
			m_handler.sendMessage(msg);
		}
	}

	@Override
	public void setHandler(Handler handler) {
		m_handler=handler;
	}

	@Override
	public Handler getHandler() {
		return m_handler;
	}
}
